/*
 * JniHFileStart.java  0.4.0 / Nov 6, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.tools.ng.jni;

import java.io.IOException;
import java.io.PrintWriter;

import com.thoughtworks.qdox.model.DocletTag;
import com.thoughtworks.qdox.model.JavaClass;

import com.cute4j.tools.ng.AbstractNativeFile;
import com.cute4j.tools.ng.NativeFileText;
import com.cute4j.tools.ng.Utils;

/**
 * JNI Header File Start.
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public class JniCppFileStart extends NativeFileText {

// Private Fields. -------------------------------------------------------------

  private final JavaClass javaClass;

// Public Constructors. --------------------------------------------------------

  /**
   * Constructs new JniCppFileStart object.
   *
   * @param nativeFile the native file
   * @param javaClass the qdox java class
   */
  public JniCppFileStart(AbstractNativeFile nativeFile, JavaClass javaClass) {
    super(nativeFile);
    this.javaClass = javaClass;
  }

// Overridden Methods. ---------------------------------------------------------

  /**
   * {@inheritDoc}
   */
  protected void storeContent(PrintWriter pw) throws IOException {
    pw.print("#include \"");
    pw.print(Utils.getMangledClassName(javaClass));
    pw.println(".h\"");
    pw.println();
    DocletTag tag = javaClass.getTagByName("cute4j.native");
    String includes = null;
    if (tag != null) {
      includes = tag.getNamedParameter("includes");
    }
    if (includes == null) {
      pw.print("#include \"");
      pw.print(javaClass.getName() + "Delegate");
      pw.println(".h\"");
    } else {
      String[] include = includes.split(",");
      for (int i = 0; i < include.length; i++) {
        String header = include[i];
        if (header.lastIndexOf('.') >= 0) {
          pw.print("#include \"");
          pw.print(header);
          pw.println("\"");
        } else {
          pw.print("#include <");
          pw.print(header);
          pw.println(">");
        }
      }
    }
    pw.println();
    pw.println("/*");
    pw.print(" * ");
    pw.print("Native implementation for: ");
    pw.println(javaClass.getFullyQualifiedName());
    pw.println(" * ");
    pw.println(" * WARNING: DO NOT EDIT THIS FILE - it is machine generated");
    pw.println(" */");
    pw.println();
  }

}
